package com.mio.hbase;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.apache.hadoop.hbase.util.Bytes;

public class DeleteProcessor extends BaseRegionObserver {

	private static final String TABLE_NAME = "userfriends";
	private static final String COL_FAMILY_NAME = "friends";
 
	
	
	@Override
	public void postDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit,
			Durability durability) throws IOException {
		// 首先查看是否对于friends列族进行了删除
		List<Cell> cells = delete.getFamilyCellMap().get(Bytes.toBytes(COL_FAMILY_NAME));
		if (cells == null || cells.size() == 0) {
			System.out.println("NO F famally operation ,just do it.");
			return;
		}

		for (Cell cell : cells) {
//			HbaseUtil.deleteData(cell.getQualifierArray(), cell.getRowArray());
			System.out.println("delete");
			byte[] kuid = cell.getQualifier();
			byte[] fuid = cell.getRow();
			if(HbaseUtil.conf == null) {
				HbaseUtil.init();
			}
			if(HbaseUtil.isColExist(kuid,fuid)) {
				System.out.println("start to delete......");
				HbaseUtil.deleteData(kuid,fuid);
			}
			HbaseUtil.destroy();
		}
	}
//
//	@SuppressWarnings("deprecation")
//	@Override
//	public void preDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit,
//			Durability durability) throws IOException {
//
//	
//		
//		
//
//	}
	
	public static void main(String[] args) throws IOException {
		byte[] kuid = Bytes.toBytes("uid1");
		byte[] fuid = Bytes.toBytes("uid3");
		if(HbaseUtil.conf == null) {
			HbaseUtil.init();
		}
		if(HbaseUtil.isColExist(kuid,fuid)) {
			System.out.println("start to delete......");
			HbaseUtil.deleteData(kuid,fuid);
		}
		System.out.println("delete end........");
		HbaseUtil.destroy();
	}
   

}
